﻿// Example header text. Can be configured in the options.
using System;
using System.Data;
using System.Windows.Forms;
using Telerik.WinControls;
using vtech_formBase;
using vtech_frameWorks.Exceptions;

namespace vtech_system.Role
{
    public partial class frmRole : frmListWithGrid
    {
        public frmRole()
        {
            this.InitializeComponent();
        }

        #region LoadData
        protected override void LoadData()
        {
            try
            {
                ClsRoleBS obj = new ClsRoleBS();
                this.dsRole.DtRole.Clear();
                this.dsRole.DtRole.Merge(obj.GetAll());
            }
            catch (Exception ex)
            {
                ClsInformation.Error(ex.ToString());
            }
        }

        private void frmRole_Load(object sender, EventArgs e)
        {
            base.frmListWithGrid_Load(sender, e);
        }

        #endregion

        protected override void Add()
        {
            try
            {
                using (frmAddRole fAdd = new frmAddRole())
                {
                    fAdd.OnAdd += LoadData;
                    fAdd.ShowDialog();
                }
            }
            catch (Exception ex)
            {
                ClsInformation.Error(ex.ToString());
            }
        }

        protected override void Edit()
        {
            try
            {
                this.ID = (Guid)this.dgvRole.SelectedRows[0].Cells[0].Value;
                using (frmEditRole fEdit = new frmEditRole(this.ID))
                {
                    fEdit.OnEdit += LoadData;
                    fEdit.ShowDialog();
                }
            }
            catch (Exception ex)
            {
                ClsInformation.Error(ex.ToString());
            }
        }

        protected override void Delete()
        {
            try
            {
                ClsRoleBS obj = new ClsRoleBS();
                if (this.dgvRole.SelectedRows.Count != 0)
                {
                    this.ID = new Guid(this.dgvRole.SelectedRows[0].Cells[0].Value.ToString());
                    if (DialogResult.Yes == RadMessageBox.Show("Bạn muốn xóa quyền này ?", "Thông báo", MessageBoxButtons.YesNo, RadMessageIcon.Question))
                    {
                        DataRow[] drow;
                        drow = obj.GetAll().Select(String.Format("ID = '{0}'", this.ID));
                        foreach (DataRow row in drow)
                        {
                            obj.DoDelete(row);
                        }
                        this.LoadData();
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    ClsInformation.Error("Chưa chọn bản ghi.Không thể xóa!");
                    return;
                }
            }
            catch (Exception ex)
            {
                ClsInformation.Error(ex.ToString());
            }
        }

        private void dgvRole_DoubleClick(object sender, EventArgs e)
        {
            this.Edit();
        }
    }
}